﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace LogoFX.Controls
{
    public class NotificationContainerPanel : Panel
    {
        #region Fields

        private static readonly Type s_thisType = typeof(NotificationContainerPanel);

        #endregion

        #region Constructors

        public NotificationContainerPanel()
        {
            //AddHandler();
        }

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty DisappearHeightProperty =
            DependencyProperty.RegisterAttached(
                "DisappearHeight",
                typeof(double),
                s_thisType,
                new PropertyMetadata(
                    0d,
                    DisappearHeight_Changed));

        public static double GetDisappearHeight(DependencyObject obj)
        {
            return (double) obj.GetValue(DisappearHeightProperty);
        }

        public static void SetDisappearHeight(DependencyObject obj, double value)
        {
            obj.SetValue(DisappearHeightProperty, value);
        }

        #endregion

        #region Public Properties

        #endregion

        #region Overrides

        protected override Size MeasureOverride(Size availableSize)
        {
            foreach (UIElement child in Children)
            {
                child.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
            }

            return base.MeasureOverride(availableSize);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (!Children.Any())
            {
                return base.ArrangeOverride(finalSize);
            }

            UIElement rootVisual = Application.Current.RootVisual;
            Size rootSize = rootVisual.RenderSize;
            Point position = TransformToVisual(rootVisual).Transform(new Point());

            double top = rootSize.Height - position.Y;

            foreach (UIElement child in Children)
            {
                Rect rect;

                double disappearedHeight = GetDisappearHeight(child);

                if (disappearedHeight > 0d)
                {
                    top -= disappearedHeight;
                    rect = new Rect(0, 0, 0, 0);
                }
                else
                {
                    Size childSize = child.DesiredSize;
                    top -= childSize.Height;

                    double left = 0d;

                    if (left + position.X + childSize.Width > rootSize.Width)
                    {
                        left = rootSize.Width - position.X - childSize.Width;
                    }

                    rect = new Rect(left, top, childSize.Width, childSize.Height);
                }

                child.Arrange(rect);
            }

            return finalSize;
        }

        #endregion

        #region Private Members

        private static void DisappearHeight_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NotificationContainerPanel panel = d.FindAncestor<NotificationContainerPanel>();

            if (panel == null)
            {
                return;
            }

            panel.InvalidateArrange();
        }

        #endregion
    }
}
